package cn.itcast.bos.base.action;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

import cn.itcast.bos.domain.base.Area;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class BaseAction <T> extends ActionSupport implements ModelDriven<T>{
	public BaseAction(){
		// TODO Auto-generated method stub
		Class<? extends BaseAction> clazz = this.getClass();
		ParameterizedType parameterizedType=(ParameterizedType) clazz.getGenericSuperclass();
		Type[] types = parameterizedType.getActualTypeArguments();
		Class type =(Class) types[0];
		try {
			t=(T) type.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	protected int rows;
	protected int page;
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	public void setPage(int page) {
		this.page = page;
	}
	protected T t;
	@Override
	public T getModel() {
	
		return t;
	}
	
	public void putIntoValueStack(Page<T> page){
		Map map=new HashMap<String, Object>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		ActionContext.getContext().getValueStack().push(map);
	}
	
}
